import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '../views/layout'

// constantRouter 代表那些不需要动态判断权限的路由
export const constantRoutes = [
  { path: '/login', component: () => import('@/views/login/index'), hidden: true },
  { path: '/resetpwd', component: () => import('@/views/login/resetPwd'), hidden: true },
  { path: '/404', component: () => import('@/views/404'), hidden: true },
  {
    path: '/svgIcon',
    name: 'svgIcons',
    component: Layout,
    redirect: '/svgIcon/list',
    hidden: true,
    children: [{
      path: 'list',
      name: 'svgIconList',
      component: () => import('@/views/svg-icons/index'),
      meta: { title: 'svg 图标' }
    }]
  },
  {
    path: '/basicsManage',
    name: 'basicsManage',
    component: Layout,
    redirect: '/basicsManage/index',
    hidden: false,
    children: [{
      path: 'index',
      name: 'basicsManage',
      component: () => import('@/views/basicsManage/index'),
      meta: { title: '基础管理' }
    }]
  },
  {
    path: '/courseManage',
    name: 'courseManage',
    component: Layout,
    meta: { title: '课程管理' },
    hidden: false,
    children: [
      {
        path: 'courseClassification',
        name: 'courseClassification',
        component: () => import('@/views/courseManage/classification'),
        meta: { title: '课程分类' }
      },
      {
        path: 'courseManage',
        name: 'courseManage',
        component: () => import('@/views/courseManage/courseManage'),
        meta: { title: '课程管理' }
      },
    ]
  },
  {
    path: '/courseDetail',
    name: 'courseDetail',
    component: Layout,
    redirect: '/courseDetail/index',
    hidden: true,
    children: [{
      path: 'index',
      name: 'courseDetail',
      component: () => import('@/views/courseManage/courseDetail'),
      meta: { title: '课程详情' }
    }]
  },
  {
    path: '/roleManage',
    name: 'roleManage',
    component: Layout,
    redirect: '/roleManage/index',
    hidden: false,
    children: [{
      path: 'index',
      name: 'roleManage',
      component: () => import('@/views/roleManage/index'),
      meta: { title: '角色管理' }
    }]
  },
  {
    path: '/userManage',
    name: 'userManage',
    component: Layout,
    redirect: '/userManage/index',
    hidden: false,
    children: [{
      path: 'index',
      name: 'userManage',
      component: () => import('@/views/userManage/index'),
      meta: { title: '成员管理' }
    }]
  },
  {
    path: '/evaluateManage',
    name: 'evaluateManage',
    component: Layout,
    redirect: '/evaluateManage/index',
    hidden: false,
    children: [{
      path: 'index',
      name: 'evaluateManage',
      component: () => import('@/views/evaluateManage/index'),
      meta: { title: '评论管理' }
    }]
  },
]
// asyncRouter 代表那些需求动态判断权限并通过 addRoutes 动态添加的页面。
export const asyncRoutes = {
  'heze': [],
  'fuzhou': []
}

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

router.$addRoutes = (params) => {
  // router.matcher = new Router({mode: 'history'}).matcher;
  router.matcher = createRouter().matcher;
  router.addRoutes(params)
}

export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

export default router
